﻿using CJFirewall.Kernel;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace CJFirewall.View
{
    /// <summary>
    /// AccessControlList.xaml 的交互逻辑
    /// </summary>
    public partial class AccessControlList : UserControl
    {
        Process main = null;
        string ACL = null;
        public AccessControlList()
        {
            InitializeComponent();
            var newF=File.OpenWrite(AppDomain.CurrentDomain.BaseDirectory + "ACLList.txt");
            newF.Close();
            ACL = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory +"ACLList.txt");
            ACLListText.Text= ACL;
            ENDB.IsEnabled = false;
            Tips.Content = "ACL控制进程暂未开启！\n可以更改规则并保存\n如需使用请开启功能。";
            Tips.Foreground = new SolidColorBrush(Colors.DarkOrange);

        }

        private void Run_Click(object sender, RoutedEventArgs e)
        {
            //main =Process.Start(AppDomain.CurrentDomain.BaseDirectory + "ConsoleApplication1.exe", "\"outbound and tcp.DstPort == 80\" 1000");
            //main.Id;
            main =Process.Start(AppDomain.CurrentDomain.BaseDirectory + "ACLKernel.exe", File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "ACLList.txt"));
            DispatcherTimer Timer = new DispatcherTimer();
            Timer.Interval = TimeSpan.FromSeconds(2);
            Timer.Tick += FindACLAPP;
            Timer.Start();
        
            EdB.IsEnabled = false;
            RunB.IsEnabled = false;
            ACLListText.IsEnabled = false;
            ENDB.IsEnabled = true;

            Tips.Content = "ACL控制进程已经开启！\n如需更改规则请暂停\n正在进行包过滤...";
            Tips.Foreground = new SolidColorBrush(Colors.GreenYellow);
        }
        void FindACLAPP(object sender, EventArgs e)
        {
            Process[] All = Process.GetProcesses();
            foreach(var ps in All)
            {
                if(ps.ProcessName== "ACLKernel")
                {
                    return;
                }
            }
            (sender as DispatcherTimer).Stop();
            EXIT();
        }
        void EXIT()
        {
            Tips.Content = "ACL控制进程已经退出！\n服务未运行！";
            Tips.Foreground = new SolidColorBrush(Colors.Red);
            main = null;
            EdB.IsEnabled = true;
            RunB.IsEnabled = true;
            ENDB.IsEnabled = false;
            ACLListText.IsEnabled = true;
        }
        private void Exit_Click(object sender, RoutedEventArgs e)
        {
            if (main != null)
            {
                try { main.Kill(); } catch (Exception) { }
            }
            main = null;
            EdB.IsEnabled = true;
            RunB.IsEnabled = true;
            ENDB.IsEnabled = false;
            ACLListText.IsEnabled = true;
        }

        private void ACLlist_Click(object sender, RoutedEventArgs e)
        {
            File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "ACLList.txt", ACLListText.Text);
        }

        private void Log_Click(object sender, RoutedEventArgs e)
        {
            
            Process.Start("Explorer", AppDomain.CurrentDomain.BaseDirectory+"log\\");
        }
    }
}
